package com.mc.cloud.mall.user.controller;

import com.mc.cloud.mall.common.common.Constant;
import com.mc.cloud.mall.common.common.MallRestResponse;
import com.mc.cloud.mall.common.exception.MallException;
import com.mc.cloud.mall.common.exception.MallExceptionEnum;
import com.mc.cloud.mall.user.model.pojo.User;
import com.mc.cloud.mall.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.security.NoSuchAlgorithmException;

/**
 * @ClassName UserController
 * @Author MC
 * @Date 2021/5/20 16:53
 * @Version 1.0
 **/
@Controller
public class UserController {
    
    @Autowired
    UserService userService;
    
    /**
     * 用户注册
     * @param userName
     * @param password
     * @return
     * @throws MallException
     */
    @ApiOperation("用户注册")
    @ResponseBody
    @PostMapping("/register")
    public MallRestResponse register(@RequestParam("userName") String userName, @RequestParam("password") String password) throws MallException {
        
        if (StringUtils.isEmpty(userName)){
            return MallRestResponse.error(MallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)){
            return MallRestResponse.error(MallExceptionEnum.NEED_PASSWORD);
        }
        //密码长度不能小于8位
        if (password.length()<8){
            return MallRestResponse.error(MallExceptionEnum.PASSWORD_TOO_SHORT);
        }
        
        userService.register(userName,password);
        
        return MallRestResponse.success();
        
    }
    
    /**
     * 普通用户登录
     * @param userName
     * @param password
     * @param session
     * @return
     * @throws MallException
     * @throws NoSuchAlgorithmException
     */
    @ApiOperation("普通用户登录")
    @ResponseBody
    @PostMapping("/login")
    public MallRestResponse login(@RequestParam("userName") String userName, @RequestParam("password") String password, HttpSession session) throws MallException, NoSuchAlgorithmException {
        
        if (StringUtils.isEmpty(userName)){
            return MallRestResponse.error(MallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)){
            return MallRestResponse.error(MallExceptionEnum.NEED_PASSWORD);
        }
        
        //用户登录
        User user = userService.login(userName, password);
        // 为了安全，不保存密码
        user.setPassword(null);
        session.setAttribute(Constant.MALL_USER,user);
        return MallRestResponse.success(user);
        
    }
    
    /**
     * 用户更新
     * @param session
     * @param signature
     * @return
     * @throws MallException
     */
    @ApiOperation("用户更新")
    @ResponseBody
    @PostMapping("/user/update")
    public MallRestResponse update(HttpSession session,@RequestParam("signature")String signature) throws MallException {
        User current_user = (User)session.getAttribute(Constant.MALL_USER);
        
        if (current_user==null){
            throw new MallException(MallExceptionEnum.NEED_LOGIN);
        }
        User user = new User();
        user.setId(current_user.getId());
        user.setPersonalizedSignature(signature);
        
        userService.update(user);
        
        return MallRestResponse.success();
        
    }
    
    /**
     * 用户登出
     * @param session
     * @return
     */
    @ApiOperation("用户登出")
    @ResponseBody
    @PostMapping("/user/logout")
    public MallRestResponse logout(HttpSession session){
        session.removeAttribute(Constant.MALL_USER);
        
        return MallRestResponse.success();
    }
    
    /**
     * 管理员登录
     * @param userName
     * @param password
     * @param session
     * @return
     * @throws MallException
     * @throws NoSuchAlgorithmException
     */
    @ApiOperation("管理员登录")
    @ResponseBody
    @PostMapping("/adminLogin")
    public MallRestResponse adminLogin(@RequestParam("userName") String userName, @RequestParam("password") String password, HttpSession session) throws MallException, NoSuchAlgorithmException {
        
        if (StringUtils.isEmpty(userName)){
            return MallRestResponse.error(MallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)){
            return MallRestResponse.error(MallExceptionEnum.NEED_PASSWORD);
        }
        
        //用户登录  判断是否为管理员
        User user = userService.login(userName, password);
        if (userService.isAdmin(user)) {
            // 为了安全，不保存密码
            user.setPassword(null);
            session.setAttribute(Constant.MALL_USER,user);
            return MallRestResponse.success(user);
        }else{
            return MallRestResponse.error(MallExceptionEnum.NEED_ADMIN);
        }
        
    }
    
    /**
     *
     * @param user
     * @return
     */
    @ApiOperation("检查是否为管理员")
    @PostMapping("/isAdmin")
    @ResponseBody
    public Boolean isAdmin(@RequestBody User user){
        return userService.isAdmin(user);
    }
    
    /**
     *
     * @param session
     * @return
     */
    @GetMapping("/getUser")
    @ResponseBody
    public User getUser(HttpSession session){
    
        User currentUser = (User)session.getAttribute(Constant.MALL_USER);
        return currentUser;
        
    }
}
